package com.xsound.dp15.bluetooothmonitor.BtToolKit;

import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.os.ParcelUuid;
import android.util.Log;

import java.lang.reflect.Method;
import java.util.NoSuchElementException;

/**
 * Created by DP15 on 16-3-15.
 */
public class BtTool {
    private Context context;
    private static final String BTtoolTAG="BtTool------>>>>>>>";

    public static boolean boundStateToBlooean(int state){
        switch (state){
            case BluetoothDevice.BOND_NONE:
                return false;
            case BluetoothDevice.BOND_BONDED:
                return true;
            default:
                return false;
        }
    }

    public static boolean boundDevice(BluetoothDevice device)throws Exception{
        Method createBondMethod = device.getClass().getMethod("createBond");
        Boolean returnValue = (Boolean) createBondMethod.invoke(device);
        return returnValue.booleanValue();
    }

    public static boolean removeBound(BluetoothDevice device)throws Exception{
        Method removeBondMethod  = device.getClass().getMethod("removeBond");
        Boolean returnValue = (Boolean) removeBondMethod .invoke(device);
        return returnValue.booleanValue();
    }

    public static String getBtClass(int deviceclass){
        String classstr;
        switch (deviceclass){
            case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE:
                classstr="免提设备";
                break;
            case BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES:
                classstr="头戴耳机";
                break;
            case BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET:
                classstr="可穿戴耳机";
                break;
            case BluetoothClass.Device.COMPUTER_DESKTOP:
                classstr="桌面电脑";
                break;
            case BluetoothClass.Device.COMPUTER_LAPTOP:
                classstr="笔记本电脑";
                break;
            case BluetoothClass.Device.COMPUTER_WEARABLE:
                classstr="可穿戴电脑";
                break;
            case BluetoothClass.Device.PHONE_CELLULAR:
                classstr="移动电话";
                break;
            case BluetoothClass.Device.PHONE_CORDLESS:
                classstr="无绳电话";
                break;
            case BluetoothClass.Device.PHONE_SMART:
                classstr="智能手机";
                break;
            case BluetoothClass.Device.WEARABLE_WRIST_WATCH:
                classstr="可穿戴手表";
                break;
            default:
                classstr="未知设备";
                break;
        }
        return classstr;
    }

    public static String getMajorClassStr(int majordenum){

        String majorstr="";
        switch (majordenum){
            case BluetoothClass.Device.Major.AUDIO_VIDEO:
                majorstr="音视频";
                break;
            case BluetoothClass.Device.Major.COMPUTER:
                majorstr="电脑";
                break;
            case BluetoothClass.Device.Major.HEALTH:
                majorstr="健康设备";
                break;
            case BluetoothClass.Device.Major.MISC:
                majorstr="MISC设备";
                break;
            case BluetoothClass.Device.Major.IMAGING:
                majorstr="图像设备";
                break;
            case BluetoothClass.Device.Major.NETWORKING:
                majorstr="网络设备";
                break;
            case BluetoothClass.Device.Major.TOY:
                majorstr="玩具设备";
                break;
            case BluetoothClass.Device.Major.PHONE:
                majorstr="手机";
                break;
            case BluetoothClass.Device.Major.PERIPHERAL:
                majorstr="外围设备";
                break;
            default:
                majorstr="未分类设备";
                break;
        }
        return majorstr;
    }

    public static String getBtTypeStr(int type){
        String typestr;
        switch (type){
            case BluetoothDevice.DEVICE_TYPE_CLASSIC:
                typestr="经典BR/EDR设备";
                break;
            case BluetoothDevice.DEVICE_TYPE_LE:
                typestr="低功耗设备";
                break;
            case BluetoothDevice.DEVICE_TYPE_DUAL:
                typestr="双模设备（BR/EDR/LE）";
                break;
            case BluetoothDevice.DEVICE_TYPE_UNKNOWN:
                typestr="未知设备";
                break;
            default:
                typestr="未知设备";
                break;
        }
        return typestr;
    }

    public static ParcelUuid[] getRemoteUuids(BluetoothDevice device) throws Exception{
        ParcelUuid[] btUuids={};
        Method method=null;
        try {
             method = device.getClass().getMethod("getUuids", new Class[0]);
        } catch (NoSuchMethodException e) {
            Log.e(BTtoolTAG,"没有getUuids方法");
            e.printStackTrace();
        }
        btUuids=(ParcelUuid[]) method.invoke(device, new Object[]{});
        return btUuids;
    }

}
